//
//  RangeSlider.m
//  RangeSlider
//
//  Created by Mal Curtis on 5/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RangeSlider.h"

@interface RangeSlider (PrivateMethods)
-(float)xForValue:(float)value;
-(float)valueForX:(float)x;
-(void)updateTrackHighlight;
@end

@implementation RangeSlider

@synthesize minimumValue, maximumValue, minimumRange, selectedMinimumValue, selectedMaximumValue;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _minThumbOn = false;
        _maxThumbOn = false;
        _padding = 20;
        
        //底图横向拉伸
        UIImage *imageTrackBackground = [[UIImage imageNamed:@"slider-background.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
        _trackBackground = [[[UIImageView alloc] initWithImage:imageTrackBackground] autorelease];
        _trackBackground.frame = CGRectMake(0, 0, frame.size.width-_padding*2, imageTrackBackground.size.height);
        _trackBackground.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        [self addSubview:_trackBackground];
        
        UIImage *imageTrack = [[UIImage imageNamed:@"slider-background-highlight.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
        _track = [[[UIImageView alloc] initWithImage:imageTrack] autorelease];
        _track.frame = _trackBackground.frame;
        [self addSubview:_track];
        
        
        UIImage* imageMinBar = [UIImage imageNamed:@"slider_bar_up_arrow.png"];
        _minThumb = [[[UIImageView alloc] initWithImage:imageMinBar highlightedImage:[UIImage imageNamed:@"slider_bar_up_arrow_pressed.png"]] autorelease];
        _minThumb.frame = CGRectMake(0,_track.frame.origin.y, imageMinBar.size.width,imageMinBar.size.height);
        [self addSubview:_minThumb];
        
        
        UIImage* imageMaxBar = [UIImage imageNamed:@"slider_bar_down_arrow.png"];
        _maxThumb = [[[UIImageView alloc] initWithImage:imageMaxBar highlightedImage:[UIImage imageNamed:@"slider_bar_down_arrow_pressed.png"]] autorelease];
        _maxThumb.frame = CGRectMake(0,_track.frame.origin.y + _track.frame.size.height - imageMaxBar.size.height, imageMaxBar.size.width,imageMaxBar.size.height);
        [self addSubview:_maxThumb];
    }
    
    return self;
}


-(void)layoutSubviews
{
    _minThumb.center = CGPointMake([self xForValue:selectedMinimumValue], _minThumb.center.y);
    _maxThumb.center = CGPointMake([self xForValue:selectedMaximumValue], _maxThumb.center.y);
    
    [self updateTrackHighlight];
}

-(float)xForValue:(float)value{
    return (self.frame.size.width-(_padding*2))*((value - minimumValue) / (maximumValue - minimumValue))+_padding;
}

-(float) valueForX:(float)x{
    return minimumValue + (x-_padding) / (self.frame.size.width-(_padding*2)) * (maximumValue - minimumValue);
}

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    if(!_minThumbOn && !_maxThumbOn){
        return YES;
    }
    
    CGPoint touchPoint = [touch locationInView:self];
    if(_minThumbOn){
        _minThumb.center = CGPointMake(MAX([self xForValue:minimumValue],MIN(touchPoint.x - distanceFromCenter, [self xForValue:selectedMaximumValue - minimumRange])), _minThumb.center.y);
        selectedMinimumValue = [self valueForX:_minThumb.center.x];
        
    }
    if(_maxThumbOn){
        _maxThumb.center = CGPointMake(MIN([self xForValue:maximumValue], MAX(touchPoint.x - distanceFromCenter, [self xForValue:selectedMinimumValue + minimumRange])), _maxThumb.center.y);
        selectedMaximumValue = [self valueForX:_maxThumb.center.x];
    }
    [self updateTrackHighlight];
    [self setNeedsLayout];
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    return YES;
}

-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchPoint = [touch locationInView:self];
    
    //扩大选区
    CGRect minExpandRect = CGRectMake(_minThumb.frame.origin.x-10, _minThumb.frame.origin.y+8, _minThumb.frame.size.width + 20, _minThumb.frame.size.height);
    CGRect maxExpandRect = CGRectMake(_maxThumb.frame.origin.x-10, _maxThumb.frame.origin.y-8, _maxThumb.frame.size.width + 20, _maxThumb.frame.size.height+2);
    
    /*
    UIView* viewMin = [[UIView alloc] initWithFrame:minExpandRect];
    viewMin.backgroundColor = [UIColor redColor];
    [self addSubview:viewMin];
    
    UIView* viewMax = [[UIView alloc] initWithFrame:maxExpandRect];
    viewMax.backgroundColor = [UIColor blueColor];
    [self addSubview:viewMax];
    */
    
    if(CGRectContainsPoint(minExpandRect, touchPoint)){
        _minThumbOn = true;
        distanceFromCenter = touchPoint.x - _minThumb.center.x;
    }
    else if(CGRectContainsPoint(maxExpandRect, touchPoint)){
        _maxThumbOn = true;
        distanceFromCenter = touchPoint.x - _maxThumb.center.x;
        
    }
    return YES;
}

-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    _minThumbOn = false;
    _maxThumbOn = false;
}

-(void)updateTrackHighlight{
	_track.frame = CGRectMake(
                              _minThumb.center.x,
                              _track.center.y - (_track.frame.size.height/2),
                              _maxThumb.center.x - _minThumb.center.x,
                              _track.frame.size.height
                              );
}


@end
